<?php
/*
 * 商家模型
 * yangbin 2011-03-20
 */
if(!defined('APP_HOME')) exit('Access Denied');

class merchantMod extends lib_model{

	protected $tbl = 'merchant'; #当前模型操作的主表

	public function _init(){
		parent::_init();
	}
	/*
	 * 添加商家
	 */
	public function addMerchant($merchant){
		return $this->insert($merchant);
	}
	
	/*
	 * 对各字段的过滤， 用于过滤客户端提交来的数据
	 * 只对字段类型做处理，对长度不做处理，请在控制器做长度的处理
	 */
	public function filterItem($item)
	{
		if(empty($item)) return array();
		
		if(isset($item['username'])) $item['username'] = filterHtml(trim($item['username']));
		if(isset($item['password'])) $item['password'] = trim($item['password']);
		if(isset($item['title'])) $item['title'] = filterHtml(trim($item['title']));
		if(isset($item['homepage'])) $item['homepage'] = trim($item['homepage']);
		if(isset($item['bankname'])) $item['bankname'] = filterHtml(trim($item['bankname']));
		if(isset($item['bankno'])) $item['bankno'] = abs(intval($item['bankno']));
		if(isset($item['bankuser'])) $item['bankuser'] = filterHtml(trim($item['bankuser']));
		if(isset($item['contact'])) $item['contact'] = filterHtml(trim($item['contact']));
		if(isset($item['address'])) $item['address'] = filterHtml(trim($item['address']));
		if(isset($item['notes'])) $item['notes'] = filterHtml(trim($item['notes']));
		if(isset($item['addip'])) $item['addip'] = filterHtml(trim($item['addip']));
		if(isset($item['lastip'])) $item['lastip'] = filterHtml(trim($item['lastip']));
		
		return $item;
	}
	
	 /*
    #修改商家信息
    */
    public function editMerchant($merchant, $id = 0)
    {
		if($id) $this->setId($id);
		elseif(!empty($merchant['id'])) $this->setId($merchant['id']);
		if(isset($merchant['id'])) unset($merchant['id']);

		return $this->update($merchant);
    }
    
 	/*
    #删除商家信息
    */
    public function delMerchant($id)
    {
		return $this->setId($id)->delete();
    }
	
	/*
	#设置商家ID
	*/
	public function setId($d, $c = 'is')
	{
		$this->setWhere($this->tbl . '.id', $d, $c);
        return $this;
	}

}